package cn.oa.service.impl;

import cn.oa.dao.PositionDao;
import cn.oa.entity.Position;
import cn.oa.service.PositionService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/3/29 9:06
 **/
public class   PositionServiceImpl implements PositionService {
	PositionDao positionDao = null;
	SqlSession sqlSession = null;

	@Override
	public List<Position> getAllPositions() throws SQLException {
		List<Position> list = null;

		try {
			String resource = "mybatis-config.xml";
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = factory.openSession();
			positionDao = sqlSession.getMapper(PositionDao.class);

			list = positionDao.getAllPositions();
		} catch ( IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}

		return list;
	}
}


